<?php
// 数据库配置
$host = 'localhost';
$dbname = 'itops_help1_center';
$username = 'itops_help1_center';
$password = 'itops_help1_center';
require 'common/function.php'; // 引入排序函数

$versions = [];
$error = '';

try {
    $pdo = new PDO(
        "mysql:host=$host;dbname=$dbname;charset=utf8mb4",
        $username,
        $password,
        [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
    );
    // 获取所有版本记录
    $stmt = $pdo->query("SELECT * FROM version_detail");
    $versions = $stmt->fetchAll(PDO::FETCH_ASSOC);
    // 按版本号降序排序（核心优化）
    usort($versions, 'compareVersions');
} catch (PDOException $e) {
    $error = "加载版本记录失败：" . $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>版本更新记录 - IT知识库</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="css/common.css"> <!-- 保持原有样式 -->
</head>
<body>
    <div class="container">
        <h1 class="page-title">版本更新记录</h1>
        
        <?php if ($error): ?>
            <div class="error"><?php echo $error; ?></div>
        <?php endif; ?>
        
        <?php foreach ($versions as $v): ?>
            <div class="version-card type-<?php echo $v['type']; ?>">
                <div class="version-header">
                    <div class="version-code"><?php echo $v['version_code']; ?></div>
                    <div class="version-date"><?php echo date('Y-m-d', strtotime($v['create_time'])); ?></div>
                </div>
                <div class="version-title"><?php echo $v['title']; ?>（<?php echo $v['type']; ?>）</div>
                <div class="version-content">
                    <?php echo nl2br(htmlspecialchars($v['content'])); ?>
                </div>
            </div>
        <?php endforeach; ?>
        
        <?php if (empty($versions) && !$error): ?>
            <div style="text-align: center; padding: 3rem; color: #64748B;">
                <i class="fas fa-history" style="font-size: 3rem; margin-bottom: 1rem; opacity: 0.5;"></i>
                <p>暂无版本记录</p>
            </div>
        <?php endif; ?>
    </div>
</body>
</html>